/*
	NAME : RUSHIKESH MUKUND FANSE
	ROLL NO : 16
	DIV :H
	ASSIGNMENT : SELECTIVE REPEAT
*/
#include<cstdio>
#include<iostream>
#include<vector>
#include<cstring>
#include<string>
#include<cstdlib>
#include<sstream>
using namespace std;
int push;
class selective_repeat{
	private:
	vector<int>v;
	int a[20];
	int windowsize;
	int last_push;
	string str;
	stringstream ss;
	int temp;
	int e,f,g;
	public:
	selective_repeat()
	{
			str="";
			push=0;
			last_push=0;
			windowsize=5;
			memset(a,'0',sizeof(int)*20);
			generate_data();
	}
	void generate_data()
	{
			for(int i=0;i<20;i++)
			{
					a[i]=i;
			}
	}
	void sender()
	{
		printf("window of frames sent to reciever..............\n");
		for(int i=0;i<windowsize;i++)
		{
			v.push_back(a[push+i]);
		}
		last_push=push;
		push=push+5;
	}
	void error(int choice)
	{
		switch(choice)
		{
				case 1:
				break;
				case 2:
					int in;
					in=rand()%5;
					e=v.at(in);
					v.at(in)=-1;
				break;
				case 3:
					int e1,e2;
					e1=rand()%5;
					e2=rand()%5;
					while(e1==e2)
					{
							e2=rand()%5;
					}
					f=v.at(e1);
					g=v.at(e2);
					v.at(e1)=-1;
					v.at(e2)=-1;
				break;
		}
	}
	void reciever(int choice)
	{
		switch(choice)
		{
			case 1:
				for(int i=0;i<windowsize;i++)
				{
					printf("frame %d recieved by reciever.............\n",v.at(i));
				}
			break;
			case 2:
				for(int i=0;i<windowsize;i++)
				{
						if(v.at(i)!=-1)
						printf("frame %d recieved by reciever..............\n",v.at(i));
						else
						{	
							printf("error in a frame :one frame is not recieved correctly............... \n");
							printf("sending acknowlegement for the frame to retransmit\n");
							printf("acknowlegement \
									\n1.lost due to time out or disturbance\
									\n2.recieved by sender\n");
									cin>>choice;
						switch(choice)
						{
							case 1:
						/*		ss<<(last_push+i);
								printf("resending the negative acknolegement ...........\n");
								printf("negative acknowlegement recieved by sender..............\n");
								str+="frame "+ss.str()+" recieved by reciever...............\n";
								ss.str("");
						*/
								printf("resending the window from frame %d to %d.......\n",push-5,push-1);
							break;
							case 2:
								ss<<(last_push+i);
								printf("sends acknowlegement to sender for the retransmission of frame %d ..............\n",e);
								str+="frame "+ss.str()+" recieved by reciever...............\n";
								ss.str("");
							break;
						}
					}
				}
				cout<<str;
				str="";
			break;
			case 3:
				int k=0;
				int framef=0;
				for(int i=0;i<windowsize;i++)
				{
					if(v.at(i)!=-1)
					printf("frame %d recieved by reciever...............\n",v.at(i));
					else
					{
							if(framef==0)
								printf("sending acknowlegement for frame %d\n",f);
							else 	
								printf("sending acknowlegement for frame %d\n",g);
								
							printf("acknowlegement \
							\n1.lost due to time out or disturbance\
							\n2.recieved by sender\n");
							cin>>choice;
						switch(choice)
						{
							case 1:
							/*	ss<<(last_push+i);
								printf("resending the negative acknolegement ...........\n");
								printf("negative acknowlegement recieved by sender..............\n");
								
								str+="frame "+ss.str()+" recieved by reciever...............\n";
								ss.str("");*/
								printf("resending the window from frame %d to %d.......\n",push-5,push);
							break;
							case 2:
								ss<<(last_push+i);
								if(framef==0)
								{
									printf("sends the acknowlegement to retransmit frame %d..............\n",f);
									framef=1;
								}
								else if(framef==1)
									printf("sends the acknowlegement to retransmit frame %d..............\n",g);
								str+="frame "+ss.str()+" recieved by reciever...............\n";
								ss.str("");
							break;
						}
					}
				}
				cout<<str;
				str="";
			break;
		}
		printf("windowsize of packets successfully recieved by reciever.............\n");
				printf("acknowlegement sent to request next window\n");
				printf("acknowlegement \
						\n1.not reached due to disturbance or time out\
						\n2.recieved\n");
				cin>>choice;
				switch(choice)
				{
					case 1:
						/*printf("resending the positive acknolegement...........\n");
						printf("positive acknowlegement recieved by sender..............\n");*/
						printf("resending the packets from frame %d to %d...........\n",push-5,push-1);
					break;
					case 2:
						printf("acknowlegement recieved by sender..............\n");
					break;
				}
		v.clear();
	}
};
int main(int argc, char const *argv[])
{
	int choice;
	selective_repeat s;
	int window_to_be_sent=4;
	while(window_to_be_sent--)
	{
		s.sender();
		printf("error introduced\n1.NO ERROR\n2.SINGLE PACKET\n3.MULTIPLE ERROR\n");
		scanf("%d",&choice);
		s.error(choice);
		printf("window recieved at reciever side\n");
		s.reciever(choice);
	}
	return 0;
}
/*
OUTPUT:
ishi@rishi-PC:~$ g++ selectiverepeat.cpp 
rishi@rishi-PC:~$ ./a.out
window of frames sent to reciever..............
error introduced
1.NO ERROR
2.SINGLE PACKET
3.MULTIPLE ERROR
1
window recieved at reciever side
frame 0 recieved by reciever.............
frame 1 recieved by reciever.............
frame 2 recieved by reciever.............
frame 3 recieved by reciever.............
frame 4 recieved by reciever.............
windowsize of packets successfully recieved by reciever.............
acknowlegement sent to request next window
acknowlegement 						
1.not reached due to disturbance or time out						
2.recieved
2
acknowlegement recieved by sender..............
window of frames sent to reciever..............
error introduced
1.NO ERROR
2.SINGLE PACKET
3.MULTIPLE ERROR
1
window recieved at reciever side
frame 5 recieved by reciever.............
frame 6 recieved by reciever.............
frame 7 recieved by reciever.............
frame 8 recieved by reciever.............
frame 9 recieved by reciever.............
windowsize of packets successfully recieved by reciever.............
acknowlegement sent to request next window
acknowlegement 						
1.not reached due to disturbance or time out						
2.recieved
1
resending the packets from frame 5 to 9...........
window of frames sent to reciever..............
error introduced
1.NO ERROR
2.SINGLE PACKET
3.MULTIPLE ERROR
2
window recieved at reciever side
frame 10 recieved by reciever..............
frame 11 recieved by reciever..............
frame 12 recieved by reciever..............
error in a frame :one frame is not recieved correctly............... 
sending acknowlegement for the frame to retransmit
acknowlegement 									
1.lost due to time out or disturbance									
2.recieved by sender
2
sends acknowlegement to sender for the retransmission of frame 13 ..............
frame 14 recieved by reciever..............
frame 13 recieved by reciever...............
windowsize of packets successfully recieved by reciever.............
acknowlegement sent to request next window
acknowlegement 						
1.not reached due to disturbance or time out						
2.recieved
2
acknowlegement recieved by sender..............
window of frames sent to reciever..............
error introduced
1.NO ERROR
2.SINGLE PACKET
3.MULTIPLE ERROR
3
window recieved at reciever side
frame 15 recieved by reciever...............
sending acknowlegement for frame 16
acknowlegement 							
1.lost due to time out or disturbance							
2.recieved by sender
2
sends the acknowlegement to retransmit frame 16..............
sending acknowlegement for frame 17
acknowlegement 							
1.lost due to time out or disturbance							
2.recieved by sender
2
sends the acknowlegement to retransmit frame 17..............
frame 18 recieved by reciever...............
frame 19 recieved by reciever...............
frame 16 recieved by reciever...............
frame 17 recieved by reciever...............
windowsize of packets successfully recieved by reciever.............
acknowlegement sent to request next window
acknowlegement 						
1.not reached due to disturbance or time out						
2.recieved
2
acknowlegement recieved by sender..............

*/